package personal.xjl._23java2studentdemo.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import personal.xjl._23java2studentdemo.mapper.TeacherMapper;
import personal.xjl._23java2studentdemo.model.Student;
import personal.xjl._23java2studentdemo.model.Teacher;
import personal.xjl._23java2studentdemo.service.TeacherService;

import java.util.List;

@Service //相当于@Controleer,@Mapper,告诉spring容器帮我创建一个service的对象
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    TeacherMapper teacherMapper;

    @Override
    public List<Teacher> findAll() {
        return teacherMapper.selectAll();
    }

    @Override
    public int remove(int id) {
        return teacherMapper.deleteById(id);
    }

    @Override
    public int add(Teacher teacher) {
        return teacherMapper.insert(teacher);
    }

    @Override
    public int edit(Teacher teacher) {
        return teacherMapper.update(teacher);
    }

    @Override
    public Teacher findOneById(int id) {
        return teacherMapper.selectById(id);
    }

    @Override
    public  List<Teacher> findByPage(int pageNo, int pageSize){
        //如果页号<=0，则从第一条记录开始，以防异常
        int fromIndex = (pageNo<1)?0:(pageNo-1)*pageSize;
        return teacherMapper.selectByPage(fromIndex, pageSize);
    }

    @Override
    public boolean login(String username, String password) {
        //如果数据库student中能查到一条记录，则返回TRUE否则返回FALSE
        return teacherMapper.selectByNameAndPwd(username,password)!=null;
    }
}
